package com.zl.test;

/**
 * geek
 * 2015/5/14
 * java实现链表
 */

class Node {
    String key;
    String name;
    int age;


    public Node() {
    }

    public Node(String key, String name, int age) {
        this.key = key;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Node{" +
                "key='" + key + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class CLType {
    Node node;
    CLType next;
}

class NodeList {

    CLType clType;

    public NodeList() {
        this.clType = init();
    }

    private CLType init() {
        CLType clType = new CLType();
        clType.next = null;
        clType.node = null;
        return clType;
    }


	 public void sortByAge() {
        CLType p = null;
        CLType q = null;
        int i = 0;
        int j = 0;
        int len = len_list();

        for (i = 0; i < len; i++) {
            CLType head = this.clType;
            for (j = 0  ; j < len-i-1; j++) {
                p = head.next;
                q = p.next;

                if(p.node.age>q.node.age) {
                    head.next = q;
                    p.next = q.next;
                    q.next = p;
                    head= q;
                }
            }
        }
    }
	
	
    public int len_list() {
        int len = 0;
        CLType p = this.clType.next;

        while (null != p) {
            p = p.next;
            len++;
        }

        return len;
    }


    public void append2(Node node) {
        CLType newNode, head_temp;

        if (null == (newNode = new CLType())) {
            System.out.println("内存分配失败");
            return;
        }

        newNode.next = null;
        newNode.node = node;

        head_temp = this.clType;

        while (null != head_temp.next) {
            head_temp = head_temp.next;
        }

        head_temp.next = newNode;
       // head_temp = newNode;

    }

    public void append(Node node) {
        insert(this.len_list() + 1, node);
    }

    public boolean insert(int pos, Node node) {
        if (null == this.clType) {
            return false;
        }
        int i = 0;
        CLType p = this.clType;

        if (null != p && i < pos - 1) {
            p = p.next;
            i++;
        }

        if (i > pos - 1 || null == p) {
            return false;
        }

        CLType temp = p.next;
        CLType cnew = new CLType();
        cnew.node = node;
        cnew.next = temp;
        p.next = cnew;
        return true;
    }

    public boolean delete(int pos) {
        CLType c = this.clType;


        int i = 0;
        while (null != c.next && i < pos - 1) {
            c = c.next;
            i++;
        }

        if (i > pos - 1 || null == c.next) {
            return false;
        }

        c.next = c.next.next;
        return true;

    }

    public void show_list() {
        CLType p = this.clType.next;

        while (null != p) {
            System.out.println(p.node);
            p = p.next;
        }
    }
}


public class List {
    public static void main(String[] args) {
        NodeList nodeList = new NodeList();
        nodeList.append2(new Node("03", "jack", 19));
        nodeList.append2(new Node("02", "lily", 14));
        nodeList.append2(new Node("01", "peter", 1));
        nodeList.append2(new Node("04", "jj", 77));
		nodeList.sortByAge();
        nodeList.show_list();
        System.out.println("delete Node");
        nodeList.delete(1);
        nodeList.show_list();
    }
}
